package com.ocean.courtyard.command;

import com.ocean.courtyard.core.datasource.SqlSessionImpl;
import com.ocean.courtyard.core.hook.HuskHomeApiHook;
import com.ocean.courtyard.entity.CourtyardHome;
import com.ocean.courtyard.exception.HomeNotFoundException;
import com.ocean.courtyard.message.Message;
import com.ocean.courtyard.util.FileUtils;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import net.william278.huskhomes.position.World;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * 回到家园
 *
 * @author zhang
 * @since 2024年11月24日
 */
public class HomeCommand implements CommandExecutor {

    private final JavaPlugin plugin;

    private final MultiverseCore core;

    private final SqlSessionImpl sqlSession;

    private final Message message;

    private final Player player;

    private final HuskHomeApiHook huskHomesAPIHook;

    public HomeCommand(JavaPlugin plugin, SqlSessionImpl sqlSession, Message message, Player player, HuskHomeApiHook huskHomesAPIHook) {
        this.plugin = plugin;
        this.core = JavaPlugin.getPlugin(MultiverseCore.class);
        this.sqlSession = sqlSession;
        this.message = message;
        this.player = player;
        this.huskHomesAPIHook = huskHomesAPIHook;
    }

    @Override
    public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
        try {
            CourtyardHome home = sqlSession.getHome(player);
            String homeServer = home.getServer();
            String playerServer = FileUtils.getServerName(plugin.getConfig());
            if (homeServer.equals(playerServer)) {
                this.teleportPlayerToCurrentServerWorld(player, home);
            } else {
                //跨服传送
                huskHomesAPIHook.teleportPlayer(player, World.from(home.getWorldName(), UUID.randomUUID()), home);
//                this.sendPlayerToServer(player, homeServer);
            }
            return true;
        } catch (HomeNotFoundException e) {
            message.sendHomeNotFoundMessage(player);
            return false;
        }
    }

    public void teleportPlayerToCurrentServerWorld(Player player, CourtyardHome home) {
        if (core.getMVWorldManager() == null) {
            plugin.getLogger().severe("MultiverseWorldManager Is Not Init！");
            return;
        }
        String worldName = home.getWorldName();
        // 检查世界是否存在
        if (core.getMVWorldManager().isMVWorld(worldName)) {
            // 获取世界
            MultiverseWorld mvWorld = core.getMVWorldManager().getMVWorld(worldName);
            Location location = new Location(mvWorld.getCBWorld(), home.getX(), home.getY(), home.getZ());
            player.teleport(location);
//                message.sendHomeTpSetMessage(player);
        } else {
            plugin.getLogger().warning("The world named " + worldName + " does not exist!");
        }
    }


    private void connectPlayer(Player player, String targetServer) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(stream);
        try {
            out.writeUTF("Connect");
            out.writeUTF(targetServer);
        } catch (IOException e) {
            plugin.getLogger().severe("Unable to send player to server: " + e.fillInStackTrace());
        }
        player.sendPluginMessage(this.plugin, "BungeeCord", stream.toByteArray());
    }

    private void sendPlayerWithMessage(Player player, String targetServer, String message) {
        // 然后发送附带消息
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(stream);
        try {
            out.writeUTF("courtyard:courtyardhomemessagechannel");
            out.writeUTF(player.getName());
            out.writeUTF(message); // 你想要发送的消息
        } catch (IOException e) {
            plugin.getLogger().severe("Unable to send player message: " + e.fillInStackTrace());
        }
        // 发送消息到BungeeCord
        player.sendPluginMessage(this.plugin, "courtyard:courtyardhomemessagechannel", stream.toByteArray());
    }
}
